package com.szhn.bookservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szhn.bookservice.entity.Book;
import com.szhn.bookservice.entity.vo.BookCondition;
import com.szhn.bookservice.mapper.BookMapper;
import com.szhn.bookservice.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szhn.common.responsejson.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 书籍 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-28
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Override
    public R getBookPageByCondition(BookCondition bookCondition, long limit, long current) {
        Page<Book> page = new Page<>(current, limit);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        String begin = bookCondition.getBegin();
        String end = bookCondition.getEnd();;
        String title = bookCondition.getTitle();

        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_creat", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_creat", end);
        }

        this.page(page, wrapper);
        List<Book> books = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total", total).data("books", books);
    }
}
